<?php
/**
 * Created by JetBrains PhpStorm.
 * User: feizheng
 * Date: 12-7-11
 * Time: 下午5:01
 * To change this template use File | Settings | File Templates.
 */

class MyCurl
{
    public function saveCookie()
    {
        $cookie_jar = tempnam('./tmp', 'cookie.txt');
        $_SESSION['cookie_jar'] = $cookie_jar;
        return $cookie_jar;
    }

    public function getCookie()
    {
        return $_SESSION['cookie_jar'];
    }

    public function setHeaders($inUrl)
    {
        $header[] = 'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, text/html, * ' . '/* ';
        $header[] = 'Accept-Language: zh-cn ';
        $header[] = 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) ';
        curl_setopt($inUrl, CURLOPT_HTTPHEADER, $header);
    }

    public function post($inUrl, $inPostFields)
    {
        $ch = curl_init();
        //set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $inUrl);
        curl_setopt($ch, CURLOPT_POST, count($inPostFields)); // 启用时会发送一个常规的POST请求，类型为：application/x-www-form-urlencoded，就像表单提交的一样。
        curl_setopt($ch, CURLOPT_POSTFIELDS, $inPostFields); // 在HTTP中的“POST”操作。如果要传送一个文件，需要一个@开头的文件名

        ob_start();
        curl_exec($ch);
        $result = ob_get_contents();
        ob_end_clean();
        return $result;
    }

    public function get($inUrl)
    {
        $ch = curl_init($inUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        return curl_exec($ch);
    }
}

;